package com.tnmi.user.config;

import com.tnmi.user.interceptor.ContextHandlerInterceptor;
import com.tnmi.user.resolver.ContextArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @version 1.0
 * @Classname LoginArgResolverConfig
 * @Description TODO
 * @Date 2023-10-21 20:21
 * @Created by tnmi
 **/
@Configuration
public class LoginArgResolverConfig implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new ContextArgumentResolver());
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(getHandlerInterceptor()!=null){
            String[] commonPathPaterns = getExcludeCommonPatterns();
            registry.addInterceptor(getHandlerInterceptor())
                    .addPathPatterns("/**")
                    .order(10)
                    .excludePathPatterns(commonPathPaterns);
            WebMvcConfigurer.super.addInterceptors(registry);
        }
    }

    private String[] getExcludeCommonPatterns() {
        String[] urls ={
            "/auth/login",
        };
        return urls;
    }

    private HandlerInterceptor getHandlerInterceptor() {
        return new ContextHandlerInterceptor();
    }
}
